﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class UserController : Controller
    {
        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
         
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblUser.GetCusUserList(GswTblUser.CurrentUser.CustomerId);
            return View(qry);
        }
        public ActionResult Create(int id = 0, string m = "", string f = "", string p = "", string fid = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
           
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var user = id == 0 ? new GswTblUser() : GswTblUser.SingleOrDefault(id);
            user.ModuleTexts = GswTblUser.CurrentUser.CustomerId == 0 ? GswTblModuleText.GetModuleList() : GswTblModuleText.GetModuleList(GswTblUser.CurrentUser.Id);
            user.UserPowers = GswTblUserPower.Fetch("where UserId=" + id);
            return View(user);
        }
        [HttpPost]
        public ActionResult Create(FormCollection c, int id = 0,  string m = "", string f = "", string p = "", string fid = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
           
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            if (GswTblUser.SingleOrDefault("where  UserName='" + c["UserName"] + "' and Id<>" + id + "") != null)
                ModelState.AddModelError("", "用户名" + c["UserName"].Trim() + "已经存在");

            var functions = GswTblFunction.Fetch("where ShowFlag=0 order by ModuleId,FuncNo");

            var flag = 0;
            if (functions.Any(x => !string.IsNullOrWhiteSpace(c["chk" + x.Id])))
                flag = 1;
            if (flag == 0)
                ModelState.AddModelError(string.Empty, "请选择用户权限");

            var user = id == 0 ? new GswTblUser() : GswTblUser.SingleOrDefault(id);
            user.CustomerId = GswTblUser.CurrentUser.CustomerId;
            user.TrueName = c["TrueName"];
            user.UserName = c["UserName"];
            user.AdminFlag = 0;
            user.RegDate = DateTime.Today;
            user.UserType = "普通用户";

            try
            {
                if (ModelState.IsValid)
                {

                    if (id == 0)
                    {
                        user.PwdSalt = GsnHelper.EncryptPwd.GenerateSalt();
                        user.PwdHash = GsnHelper.EncryptPwd.EncodePassword(c["Pwd"].Trim(), user.PwdSalt);
                        user.Insert();
                    }
                    else
                    {
                        user.Update();
                    }
                    GswTblUserPower.Delete("where UserId=@0", user.Id);
                    var userPower = new GswTblUserPower();
                    foreach (var x in from x in functions let chkName = "chk" + x.Id where !string.IsNullOrWhiteSpace(c[chkName]) select x)
                    {
                        userPower.UserId = user.Id;
                        userPower.FunId = x.Id;
                        userPower.ModuleId = x.ModuleId;
                        userPower.Insert();
                    }
                    
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            user.ModuleTexts = GswTblUser.CurrentUser.CustomerId == 0 ? GswTblModuleText.GetModuleList() : GswTblModuleText.GetModuleList(GswTblUser.CurrentUser.Id);
            user.UserPowers = GswTblUserPower.Fetch("where UserId=" + id);
            return View(user);

        }

        public ActionResult Delete(int id, string m = "", string f = "", string p = "", string fid = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
           
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblUser.Delete(id);
            var qry = GswTblUser.GetCusUserList(GswTblUser.CurrentUser.CustomerId);
            return View("Index", qry);
        }

    }
}
